package nl.saxion.act.main;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import nl.saxion.act.entity.Customer;
import nl.saxion.act.entity.Order;
import nl.saxion.act.entity.Product;


public class Main {
  private static final String PERSISTENCE_UNIT_NAME = "Saktiehuis";
  private static EntityManagerFactory factory;

  public static void main(String[] args) {
    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    EntityManager em = factory.createEntityManager();
    // Read the existing entries and write to console
    Query q = em.createQuery("select p from Product p");
	List<Product> productList = q.getResultList();
    for (Product p : productList) {
      System.out.println(p.getName());
    }
    System.out.println("Size: " + productList.size());

    // Create new product
    em.getTransaction().begin();
    Product prod1 = new Product(10, "Kaas", "Zuivel");
    Product prod2 = new Product(10, "Melk", "Zuivel");
    Product prod3 = new Product(10, "Ei", "Zuivel");
    
    Order ord1 = new Order();
    ord1.addProduct( prod1 );
    ord1.addProduct( prod2 );
    ord1.addProduct( prod3 );
    Order ord2 = new Order();
    ord2.addProduct( prod1 );
    ord2.addProduct( prod2 );
    ord2.addProduct( prod3 );
    
    Customer cust1 = new Customer( "Rick" );
    Customer cust2 = new Customer( "Fleur" );
    cust1.addOrder( ord1 );
    cust2.addOrder( ord2 );
    
    
    em.persist(cust1);
    em.persist(cust2);
    em.getTransaction().commit();
    em.close();    
  }
} 
